from multiprocessing import Pool
import time,os
def task(name):
    print(name,f'当前线程为{os.getpid()},父进程为{os.getppid()}')
    time.sleep(1)

if __name__ == '__main__':
    start=time.time()
    print("*"*20,start,"*"*20)
    # 创建线程池
    p=Pool(3)
    #创建线程
    for i in range(10):
        p.apply_async(func=task,args=(i,))
    # 关闭线程池，不再接收新任务
    p.close()
    # 阻塞主进程 等执行完计算时间
    p.join()
    print(time.time()-start)


# class MyThread(Thread):
#     def __init__(self,name):
#         super(MyThread,self).__init__()
#         self.name=name
#     def run(self):
#         for i in range(10):
#             time.sleep(1)
#             print(self.name,i,f'当前进程为{os.getpid()},父进程为{os.getppid()}')
#
#
# if __name__ == '__main__':
#     #创建线程
#     t1 = MyThread('刘秀22')
#     t2 = MyThread('赵匡胤33')
#     t3 = MyThread('李世民444')
#
#     t1.start()
#     t2.start()
#     t3.start()